package com.caimiao.miao.page.ticket;

import android.content.Context;

import com.caimiao.miao.bean.TicketOut;
import com.caimiao.miao.library.database.DBHelperFactory;
import com.caimiao.miao.library.database.table.TicketOutDBHelper;
import com.caimiao.miao.library.utils.DateUtil;
import com.caimiao.miao.library.utils.tip.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class TicketModel implements ITicketContact.Model {
    private TicketOut mTicket;
    private int mRemainCount;

    private final List<TicketOut> mStackTicket = new ArrayList<>();

    @Override
    public void setTicket(TicketOut ticket) {
        ticket.setDate(DateUtil.getNowTime());
        ticket.setType("150");
        mTicket = ticket;
        mRemainCount = Integer.valueOf(mTicket.getTicket_num());
        LogUtil.e("TICKET_OUT", "Date: " + ticket.getDate());
        //数据库备份
        DBHelperFactory.getDBHelper(TicketOutDBHelper.class).insert(ticket);
    }

    @Override
    public void setRemainCount(int remain) {
        mRemainCount = remain;
    }

    public int getRemainCount() {
        return mRemainCount;
    }

    @Override
    public TicketOut getNextTicket() {
        if (mStackTicket.isEmpty())
            return null;
        return mStackTicket.remove(0);
    }

    @Override
    public int getMaxNum() {
        return mTicket == null ? 0 : Integer.valueOf(mTicket.getTicket_num());
    }

    @Override
    public void updateBeforeClose(Context context, int state) {
        DBHelperFactory
                .getDBHelper(TicketOutDBHelper.class)
                .updateStateByTrade(mTicket.getOut_trade_no(), state);
    }

    @Override
    public void addTicket(TicketOut ticket) {
        mStackTicket.add(ticket);
    }
}
